package com.hello.system.domain.vo;

import com.hello.system.domain.RentalDevices;
import com.hello.system.domain.RentalDeviceInventory;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;

/**
 * 设备与库存信息VO
 * 
 * @author hello
 * @date 2025-07-20
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class RentalDeviceWithInventoryVo extends RentalDevices
{
    private static final long serialVersionUID = 1L;

    /** 库存信息 */
    private RentalDeviceInventory inventory;

    /** 库存状态描述 */
    private String inventoryStatus;

    /** 是否有库存 */
    private Boolean hasInventory;

    /**
     * 构造函数 - 基于设备信息创建
     */
    public RentalDeviceWithInventoryVo(RentalDevices device) {
        if (device != null) {
            // 使用BeanUtils复制属性，避免手动设置每个字段
            BeanUtils.copyProperties(device, this);
        }
    }

    /**
     * 设置库存信息并计算相关状态
     */
    public void setInventoryInfo(RentalDeviceInventory inventory) {
        this.inventory = inventory;
        this.hasInventory = inventory != null;
        this.inventoryStatus = calculateInventoryStatus(inventory);
    }

    /**
     * 计算库存状态描述
     */
    private String calculateInventoryStatus(RentalDeviceInventory inventory) {
        if (inventory == null) {
            return "无库存数据";
        }

        Long available = inventory.getAvailableQuantity() != null ? inventory.getAvailableQuantity() : 0L;
        Long total = inventory.getTotalQuantity() != null ? inventory.getTotalQuantity() : 0L;
        Long damaged = inventory.getDamagedQuantity() != null ? inventory.getDamagedQuantity() : 0L;
        Long maintenance = inventory.getMaintenanceQuantity() != null ? inventory.getMaintenanceQuantity() : 0L;

        if (total == 0) {
            return "无库存";
        } else if (available == 0) {
            if (damaged > 0 || maintenance > 0) {
                return "暂时不可用";
            } else {
                return "已租完";
            }
        } else if (available == total) {
            return "库存充足";
        } else {
            return "部分可用";
        }
    }

    /**
     * 获取可用库存数量
     */
    public Long getAvailableQuantity() {
        return inventory != null && inventory.getAvailableQuantity() != null ? 
               inventory.getAvailableQuantity() : 0L;
    }

    /**
     * 获取总库存数量
     */
    public Long getTotalQuantity() {
        return inventory != null && inventory.getTotalQuantity() != null ? 
               inventory.getTotalQuantity() : 0L;
    }

    /**
     * 获取已租出数量
     */
    public Long getRentedQuantity() {
        return inventory != null && inventory.getRentedQuantity() != null ? 
               inventory.getRentedQuantity() : 0L;
    }

    /**
     * 是否可以租赁
     */
    public Boolean isRentable() {
        return hasInventory && getAvailableQuantity() > 0;
    }
}
